package com.xiaoxu.intranetweb.mybatisplusSzappdb.business.excel;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.log.SysLogBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.SysUser;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.TExcelTemplates;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ITExcelTemplatesService;
import com.xiaoxu.intranetweb.util.SzResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.time.LocalDateTime;
import java.util.List;

@Component
public class ExcelTemplatesBu {
 @Autowired
    private ITExcelTemplatesService iTExcelTemplatesService;
 @Autowired
 private UserInfoDao userInfoDao;
    @Autowired
    private SysLogBu sysLogBu;
// 拿到所有模板
    public SzResult getAllTemplates( String token,String functionName,String plantId) throws Exception {
        QueryWrapper<TExcelTemplates> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("DATA_FORM", plantId);
        if (functionName != null && !functionName.isEmpty()) {
            queryWrapper.and(wrapper -> wrapper.like("EXCEL_FUNCTION_NAME", functionName).or().like("EXCEL_FUNCTION", functionName));
        }
        List<TExcelTemplates> list = iTExcelTemplatesService.list(queryWrapper);
        return SzResult.success(list);
    }

//    修改模型信息
    public SzResult updateExcelTemplates(String id,String excelFunctionName,String excelRemark, String token,String plantId) throws Exception {
        QueryWrapper<TExcelTemplates> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        queryWrapper.eq("DATA_FORM", plantId);
        TExcelTemplates tExcelTemplates = new TExcelTemplates();
        tExcelTemplates.setExcelFunctionName(excelFunctionName);
        tExcelTemplates.setExcelRemark(excelRemark);
        tExcelTemplates.setExcelUpdateTime(LocalDateTime.now());
        tExcelTemplates.setExcelUpdatorId(userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString()).getTrueName());
        iTExcelTemplatesService.update(tExcelTemplates, queryWrapper);
    return SzResult.Success();
    }

//    下载模板
    public TExcelTemplates downloadExcelTemplates(String id) throws Exception {
        QueryWrapper<TExcelTemplates> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        TExcelTemplates tExcelTemplates = iTExcelTemplatesService.getOne(queryWrapper);
        return tExcelTemplates;
    }
//上传新模板
    public SzResult uploadExcelTemplates(MultipartFile file, String id, String token,String plantId) throws Exception {
//        文件放到程序真在运行的根目录下/excelTemplates/，没有就创建
        File upload = new File("excelTemplates");
        if (!upload.exists()) {
            upload.mkdirs();
        }
        File dest = new File(upload.getAbsolutePath() + File.separator + id+".xlsx");
        if (dest.exists()) {
            dest.delete();
        }
        file.transferTo(dest);
        SysUser sysUser = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        QueryWrapper<TExcelTemplates> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        TExcelTemplates tExcelTemplates = iTExcelTemplatesService.getOne(queryWrapper);
        tExcelTemplates.setExcelPath(upload.getAbsolutePath());
        tExcelTemplates.setExcelUpdateTime(LocalDateTime.now());
        tExcelTemplates.setExcelUpdatorId(sysUser.getId());
        iTExcelTemplatesService.update(tExcelTemplates, queryWrapper);
        sysLogBu.logRecord("上传模板", sysUser.getTrueName(),sysUser.getTrueName()+"上传了"+tExcelTemplates.getExcelFunction()+"模板",plantId);
        return SzResult.success(tExcelTemplates);
    }

    public  SzResult createExcelTemplates(String excelFunction,String excelFunctionName,String excelFunctionFiled,String excelRemark,String token,String plantId) throws Exception {
        SysUser sysUser = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        //String plantId = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();
        TExcelTemplates tExcelTemplates = new TExcelTemplates();
        tExcelTemplates.setExcelFunction(excelFunction);
        tExcelTemplates.setExcelFunctionName(excelFunctionName);
        tExcelTemplates.setExcelFunctionFiled(excelFunctionFiled);
        tExcelTemplates.setExcelRemark(excelRemark);
        tExcelTemplates.setDataForm(plantId);
        sysLogBu.logRecord("创建模板", sysUser.getTrueName(),sysUser.getTrueName()+"创建了"+excelFunction+"模板",userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId());
        iTExcelTemplatesService.save(tExcelTemplates);
        return SzResult.Success();
    }
}
